package bzapps.weynak;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.RemoteViews;

public class WeynakWidget extends AppWidgetProvider{
	
	public static WeynakWidget Widget = null;
	public static Context context;
	public static AppWidgetManager appWidgetManager;
	public static int appWidgetIds[];
	private static SharedPreferences pref;
	public static String user, fname, lname;
	static double lat;
	static double lon;
	static ArrayList<Friend> friends;
		
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		
		if (null == context) context = WeynakWidget.context;
	    if (null == appWidgetManager) appWidgetManager = WeynakWidget.appWidgetManager;
	    if (null == appWidgetIds) appWidgetIds = WeynakWidget.appWidgetIds;
	    
	    WeynakWidget.Widget = this;
	    WeynakWidget.context = context;
	    WeynakWidget.appWidgetManager = appWidgetManager;
	    WeynakWidget.appWidgetIds = appWidgetIds;
	    
	    
	    final int N = appWidgetIds.length;
	    for (int i=0; i<N; i++) {
	        int appWidgetId = appWidgetIds[i];  
	        
	        updateAppWidget(context,appWidgetManager, appWidgetId);            
	    }


	}
	
	

	static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
    
		fetchData();
		Friend francois = new Friend();
		francois = friends.get(0);
		   Location l1 = new Location("NETWORK");
		   l1.setLongitude(Double.valueOf(pref.getString("LAST_LONGITUDE", "0")));
		   l1.setLatitude(Double.valueOf(pref.getString("LAST_LATITUDE", "0")));;
		   
		   Location l2 = new Location("NETWORK");
		   l2.setLongitude(francois.lon);
		   l2.setLatitude(francois.lat);
		   float distance = l1.distanceTo(l2);
		
		Intent intent = new Intent(context, UpdateLocationService.class);
        PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
		
		RemoteViews rview = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
		rview.setTextViewText(R.id.widget_name, user);
		rview.setTextViewText(R.id.widget_fr_name, francois.first_name + " " + francois.last_name);
		rview.setTextViewText(R.id.widget_fr_distance, String.valueOf(distance));
    // Tell the widget manager;
		appWidgetManager.updateAppWidget(appWidgetId,rview );
    
}
	
	public static void fetchData(){
		pref = PreferenceManager.getDefaultSharedPreferences(context);
		try {
			user = pref.getString("FirstName", "0")+ " " + pref.getString("LastName", "0");
			friends = ClientOperations.getFriends(user);
			for(int i=0; i<friends.size(); i++){
	  			Friend f = new Friend(); 
	  			f = friends.get(i);
	  			lat = f.lat;
	  			lon = f.lon;
	  			fname = f.first_name;
	  			lname = f.last_name;
	  			friends.add(f);
			}
			
		} catch (Exception e) {
			Log.e("WEYNAK.widget.update", e.getMessage());
		}
		
		
	}
}
